package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.TMTxnOrderMapper;
import cc.rengu.igas.momp.common.dao.impl.TMTxnOrderMapperImpl;
import cc.rengu.igas.momp.common.entity.TMTxnOrder;
import cc.rengu.igas.momp.common.enums.BizStatusEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.TxnRegulationStatusBean;
import cc.rengu.igas.momp.facade.request.QueryTxnRegulationResultRequest;
import cc.rengu.igas.momp.facade.response.QueryTxnRegulationResultResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

/**
 * 交易调账结果查询实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @since 2020/6/17 17:49
 */

public class QueryTxnRegulationResultService extends RadpService {

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryTxnRegulationResultRequest queryTxnRegulationResultRequest = new QueryTxnRegulationResultRequest();
            ConvertUtil.convertOutput(queryTxnRegulationResultRequest);

            QueryTxnRegulationResultResponse queryTxnRegulationResultResponse = new QueryTxnRegulationResultResponse();
            queryTxnRegulationResultResponse.setHeader(queryTxnRegulationResultRequest.getHeader());
            BizResponse<QueryTxnRegulationResultResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryTxnRegulationResultResponse);

            // 服务调用
            BizResponse<QueryTxnRegulationResultResponse> bizResponseNew = txnDataRegulation(xmlTreeUtil, queryTxnRegulationResultRequest, queryTxnRegulationResultResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 交易调账结果查询
     *
     * @param xmlTreeUtil                      内部XML树
     * @param queryTxnRegulationResultRequest  交易调账结果查询接口请求对象
     * @param queryTxnRegulationResultResponse 交易调账结果查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryTxnRegulationResultResponse> 交易调账结果查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @since 2020/6/19 17:58
     */
    private BizResponse<QueryTxnRegulationResultResponse> txnDataRegulation(XmlTreeUtil xmlTreeUtil, QueryTxnRegulationResultRequest queryTxnRegulationResultRequest, QueryTxnRegulationResultResponse queryTxnRegulationResultResponse) throws Exception {

        BizResponse<QueryTxnRegulationResultResponse> bizResponse = new BizResponse<>();

        int returnCode;

        TxnRegulationStatusBean txnRegulationStatusBean = queryTxnRegulationResultRequest.getOrderDetail();
        String instId = queryTxnRegulationResultRequest.getHeader().getInstId();
        String origTransDate = txnRegulationStatusBean.getOrigTxnDate();
        String origTraceNo = txnRegulationStatusBean.getOrigTraceNo();

        /* 取原交易流水 */
        TMTxnOrderMapper tmTxnOrderMapper = new TMTxnOrderMapperImpl();
        TMTxnOrder tmTxnOrder = tmTxnOrderMapper.selectTransactionOrderInfo(instId, origTransDate, origTraceNo);
        if (null == tmTxnOrder) {
            rglog.error("未取到法人机构<{}>在<{}>的流水号<{}>的原始交易信息!", instId, origTransDate, origTraceNo);
            throw new BizException(RespCodeEnum.TXN_REGULATION_FAILED_ERROR.getRespCode(), RespCodeEnum.TXN_REGULATION_FAILED_ERROR.getRespDesc());
        }

        if (BizStatusEnum.PROCESS_REGULATED.getBizStatusCode().equals(tmTxnOrder.getBizStatus())) {
            /* 原交易已调账成功 */
            txnRegulationStatusBean.setRegulationStatus(CommonConstant.REGULATION_STATUS_SUCC);

            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

            queryTxnRegulationResultResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            queryTxnRegulationResultResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        } else {
            /* 原交易不可调账 */
            txnRegulationStatusBean.setRegulationStatus(CommonConstant.REGULATION_STATUS_FAIL);


            bizResponse.setRespCode(RespCodeEnum.TXN_REGULATION_FAILED_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TXN_REGULATION_FAILED_ERROR.getRespDesc());

            queryTxnRegulationResultResponse.setRespCode(RespCodeEnum.TXN_REGULATION_FAILED_ERROR.getRespCode());
            queryTxnRegulationResultResponse.setRespDesc(RespCodeEnum.TXN_REGULATION_FAILED_ERROR.getRespDesc());
        }

        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        queryTxnRegulationResultResponse.setOrderDetail(txnRegulationStatusBean);

        bizResponse.setResult(queryTxnRegulationResultResponse);

        return bizResponse;
    }
}
